package ServiceImpl;

import Dao.DaoStuHandle;
import Model.Student;
import Service.StudentService;

import java.util.Scanner;

public class StudentServiceImpl implements StudentService {

    @Override
    public void showStu() throws Exception {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.showStuDAO();
    }

    @Override
    public void addStu(String name,String sex,Integer age,String major,String format) throws Exception {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.addStuDAO(name,sex,age,major,format);
    }

    @Override
    public boolean delStu(int stuId) throws Exception {

//        如果存在，调用数据访问层删除数据
            System.out.println("你确定要删除吗？y or n");
            Scanner scanner = new Scanner(System.in);
            String next = scanner.next();
            if (next.equals("y")){
                DaoStuHandle daoStuHandle = new DaoStuHandle();
                boolean b = daoStuHandle.delStuDAO(stuId);
                if (b){
                    System.out.println("删除成功");
                    return true;
                }else
                {
                    System.out.println("删除不存在");
                    return false;
                }
            }else {
                return false;
            }

    }

    @Override
    public boolean updateStu(int stuId1) throws Exception {
        if (true){
            DaoStuHandle daoStuHandle = new DaoStuHandle();
            daoStuHandle.updateStuDAO(stuId1);
            return true;
        }
        return false;
    }
    @Override
    public boolean searchStu(Student[] stus, int len, Integer id) {
      //判断学号是否存在
        int a = 0;//记录 学号
        boolean flag = false;
        for (Integer i = 0; i < len; i++) {
            if (stus[i].getId() == id) {
                a = i;
                flag = true;
                break;

            }
            //如果存在，就到数据链路层寻找对应的数据
        }if (flag){
            DaoStuHandle daoStuHandle = new DaoStuHandle();
            daoStuHandle.searchStuDAO(stus,len,a);
            return true;
        }
      return false;
    }
    public void findStu(Student[] stus, int len,Student[]find){
        System.out.println("请输入要查询的姓名");
        Scanner scanner = new Scanner(System.in);
        String xm  = scanner.next();
//        for (int l = 0; l < len; l++) {
//            if (xm.equals(stus[l].getName())){
//                find[UserUi.num1++]=stus[l];
//            }
//        }
    }

}
